﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Text;

namespace Microsoft.Popfly.GameCreator.GameEngine
{
    public partial class SilverlightConsole : UserControl
    {
        public event EventHandler ConsoleHidden;
        public event EventHandler ConsoleShown;

        public bool UseConsole { get; set; }
        private List<SilverlightConsoleItem> m_consoleItems = new List<SilverlightConsoleItem>();
        public SilverlightConsole()
        {
            InitializeComponent();
            Width = 500;
            Hide();
            UseConsole = false;
        }

        private int m_messageToReplace = 0;
        private const int m_maxMessages = 500;
        public void Write(String message, ConsoleFormat format)
        {
            if (UseConsole)
            {
                if (format == ConsoleFormat.Error || format == ConsoleFormat.Warning)
                {
                    Show();
                }
                if (Visibility == Visibility.Visible)
                {
                    if (m_consoleItems.Count < m_maxMessages)
                    {
                        m_consoleItems.Add(new SilverlightConsoleItem(message, format, (MessageScrollViewer.ViewportWidth == 0) ? 473 : MessageScrollViewer.ViewportWidth));
                        MessageContainer.Children.Add(m_consoleItems[m_consoleItems.Count - 1]);
                    }
                    else
                    {
                        m_consoleItems[m_messageToReplace].Message = message;
                        m_consoleItems[m_messageToReplace].Format = format;
                        MessageContainer.Children.RemoveAt(0);
                        MessageContainer.Children.Add(m_consoleItems[m_messageToReplace]);
                        m_messageToReplace++;
                        m_messageToReplace %= m_maxMessages;
                    }
                    MessageScrollViewer.ScrollToVerticalOffset(MessageScrollViewer.ExtentHeight);
                }
            }
        }

        public void Write(String message)
        {
            Write(message, ConsoleFormat.None);
        }

        public void ReportError(String message)
        {
            Write(message, ConsoleFormat.Error);
        }

        public void IssueWarning(String message)
        {
            Write(message, ConsoleFormat.Warning);
        }

        public void Show()
        {
            if (UseConsole)
            {
                this.Visibility = Visibility.Visible;
                if (ConsoleShown != null)
                {
                    ConsoleShown.Invoke(this, null);
                }
            }
        }
        public void Hide()
        {
            this.Visibility = Visibility.Collapsed;
            if (ConsoleHidden != null)
            {
                ConsoleHidden.Invoke(this, null);
            }
        }

        public string GetText()
        {
            StringBuilder builder = new StringBuilder();
            foreach (UIElement element in MessageContainer.Children)
            {
                builder.Append(((SilverlightConsoleItem)element).GetText());
                builder.Append("\n");
            }
            return builder.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Dialog dlg = new CopyableConsoleDialog(GetText());

            dlg.Show(DialogStyle.Modal);
        }
    }
}
